using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace Portal.Extensions
{
    public static class SessionExtensions
    {
        public static void Set<T>(this ISession session, string key, T value)
        {
            string json = JsonConvert.SerializeObject(value);
            byte[] serializedValue = System.Text.Encoding.UTF8.GetBytes(json);
            session.Set(key,serializedValue);
        }
        
        public static T Get<T>(this ISession session, string key)
        {
            var str = session.GetString(key);
            if (string.IsNullOrWhiteSpace(str))
                return default(T);
            return JsonConvert.DeserializeObject<T>(str);
        }
    }
}